package com.youxuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youxuan.mapper.CartDetailMapper;
import com.youxuan.pojo.entity.Cart;
import com.youxuan.pojo.entity.CartDetail;
import com.youxuan.pojo.entity.Goods;
import com.youxuan.pojo.vo.GoodsVo;
import com.youxuan.service.CartDetailService;
import com.youxuan.service.CartService;
import com.youxuan.service.GoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartDetailServiceImpl extends ServiceImpl<CartDetailMapper, CartDetail> implements CartDetailService {

    @Autowired
    private CartDetailMapper cartDetailMapper;
    @Autowired
    private CartService cartService;
    @Autowired
    private GoodsService goodsService;

    @Override
    public List<CartDetail> getCartDetailByCid(Long cartId) {
        LambdaQueryWrapper<CartDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CartDetail::getCartId, cartId);
        List<CartDetail> cartDetails = cartDetailMapper.selectList(queryWrapper);
        return cartDetails;
    }

    @Override
    public void addCartGoods(CartDetail cartDetail) {
        cartDetailMapper.insert(cartDetail);
    }

    @Override
    public CartDetail getCartDetailByCidAndGid(Long cartId, Long goodsId) {
        LambdaQueryWrapper<CartDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CartDetail::getCartId, cartId);
        queryWrapper.eq(CartDetail::getGoodsId, goodsId);
        return cartDetailMapper.selectOne(queryWrapper);
    }

    @Override
    public void changeCartGoodsNum(CartDetail cartDetail) {
        LambdaQueryWrapper<CartDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CartDetail::getCartId, cartDetail.getCartId());
        queryWrapper.eq(CartDetail::getGoodsId, cartDetail.getGoodsId());
        CartDetail cartDetailNum = new CartDetail();
        cartDetailNum.setNum(cartDetail.getNum());
        cartDetailMapper.update(cartDetailNum, queryWrapper);
    }

    @Override
    public void changeCartGoodsSelect(Long gid, Long uid,String isSelect) {
        Long cartId = this.getCartIdByUid(uid);
        LambdaQueryWrapper<CartDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CartDetail::getCartId, cartId);
        queryWrapper.eq(CartDetail::getGoodsId, gid);
        CartDetail cartDetail = new CartDetail();
        cartDetail.setIsSelect(isSelect);
        cartDetailMapper.update(cartDetail, queryWrapper);
    }

    @Override
    public void changeCartGoodsNum(Long gid, Long uid, Integer num) {
        Long cartId = this.getCartIdByUid(uid);
        CartDetail cartDetail = new CartDetail();
        cartDetail.setCartId(cartId);
        cartDetail.setGoodsId(gid);
        cartDetail.setNum(num);
        this.changeCartGoodsNum(cartDetail);
    }

    @Override
    public GoodsVo getCartGoodsDetail(Long gid, Long uid) {
        Long cartId = getCartIdByUid(uid);

        GoodsVo goodsVo = new GoodsVo();
        Goods goods = goodsService.getGoods(gid);
        BeanUtils.copyProperties(goods,goodsVo);

        LambdaQueryWrapper<CartDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CartDetail::getCartId, cartId);
        queryWrapper.eq(CartDetail::getGoodsId, gid);
        CartDetail cartDetail = cartDetailMapper.selectOne(queryWrapper);
        goodsVo.setNum(cartDetail.getNum());
        goodsVo.setIsSelect(cartDetail.getIsSelect());
        return goodsVo;
    }

    @Override
    public void deleteCartGoods(Long userId, Long goodsId) {
        Long cartId = this.getCartIdByUid(userId);
        LambdaQueryWrapper<CartDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CartDetail::getCartId, cartId);
        queryWrapper.eq(CartDetail::getGoodsId, goodsId);
        cartDetailMapper.delete(queryWrapper);
    }

    public Long getCartIdByUid(Long uid) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUid, uid);
        Cart cart = cartService.getOne(queryWrapper);
        Long cartId = cart.getId();
        return cartId;
    }


}
